#!/usr/bin/env python

import sys
import codecs
# https://docs.python.org/2/library/xml.dom.minidom.html
from xml.dom import minidom

# http://stackoverflow.com/questions/8764017/commenting-and-uncommenting-xml-via-python
def comment_node(node):
    comment = node.ownerDocument.createComment(node.toxml())
    node.parentNode.replaceChild(comment, node)
    return comment

def uncomment_node(comment):
    node = minidom.parseString(comment.data).firstChild
    comment.parentNode.replaceChild(node, comment)
    return node

def iterate_children(parent):
	child = parent.firstChild
	while child != None:
		yield child
		child = child.nextSibling

# http://stackoverflow.com/questions/38015864/python-xml-dom-minidom-please-dont-escape-my-strings
def fake_writedata(writer, data):
    if data:
        data = data.replace("&", "&amp;").replace("<", "&lt;")
        writer.write(data)

minidom._write_data = fake_writedata

def p_product_not_default(node):
    attr = node.getAttribute('product')
    return attr != None and attr != '' and attr != 'default'

def main(xmlFile):
    dom = minidom.parse(xmlFile)
    root = dom.documentElement

# http://stackoverflow.com/questions/9311337/iterate-over-element-childrens-in-python
    c = root.firstChild
    while c != None:
        if c.nodeType == c.ELEMENT_NODE:
            if p_product_not_default(c):
                c = comment_node(c)
        c = c.nextSibling

    f = codecs.open('out.xml' if len(sys.argv)<3 else sys.argv[2], 'w', 'UTF-8')
    dom.writexml(f, encoding='UTF-8')
    f.close()

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "usage: "+sys.argv[0]+" xmlFile"
        sys.exit(100)
    sys.exit(main(sys.argv[1]))